﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace SPA_Prototype.Model
{
    public enum USER_TYPE { ENGINEER, ADMIN, MOD, NORMAL_USER, NONE };
    public class AdminManagement
    {
        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.AdminTableDataTable _DataTable;
        private DatabaseDataSetTableAdapters.AdminTableTableAdapter _Adt;

        // constructor
        public AdminManagement(DatabaseDataSet dt)
        {
            _Dataset = dt;
            _DataTable = _Dataset.AdminTable;

            _Adt = new DatabaseDataSetTableAdapters.AdminTableTableAdapter();
            LoadDB();
        }

        // property / field
        public List<string> User = new List<string>();
        public List<string> Pass = new List<string>();
        public string Current_User;

        // Method : LoadDB, UpdateDB
        public void AddNewUser(string username, string pass)
        {
            DataRow NewRow = _DataTable.NewRow();
            NewRow["Id"] =  _DataTable.Rows.Count + 1;
            NewRow["User"] = username;
            NewRow["Pass"] = pass;
            NewRow["Privilege"] = 2; //_DataTable.Rows.Count;  // add mod
            NewRow["Notes"] = "Mod";

            _DataTable.Rows.Add(NewRow);
            UpdateDB();
        }

        public bool DeleteUser(string username)
        {
            bool bSucess = false;
            int count = _DataTable.Rows.Count;
            for (int j = 0; j < count; j++)
                if (_DataTable[j].User == username)
                {
                    _DataTable[j].Delete();
                    bSucess = true;
                    break;
                }
            UpdateDB();
            return bSucess;
        }

        public bool UpdatePass(string username, string pass)
        {
            bool bSuccess = false;
            int count = _DataTable.Rows.Count;
        
            for (int j = 0; j < count; j++)
                if (_DataTable[j].User == this.Current_User)
                {
                    if (username != this.Current_User)
                    {
                        _DataTable[j].User = username;
                        this.Current_User = username;
                    }
                    _DataTable[j].Pass = pass;
                    bSuccess = true;
                    break;
                }
            
            UpdateDB();
            return bSuccess;
        }
        private int FindPrevilege(string username)
        {
            int result = -1;
            for (int j = 0; j < _DataTable.Rows.Count; j++)
            {
                if (_DataTable[j].User == username)
                {
                    result = _DataTable[j].Privilege;
                    break;
                }
            }

            return result;
        }

        public USER_TYPE FindUser(string user, string pass)
        {
            USER_TYPE type = USER_TYPE.NONE;
            int count = _DataTable.Rows.Count;
            for (int j = 0; j < count; j++)         
                if ((_DataTable[j].User == user) && (_DataTable[j].Pass == pass))
                {
                    switch (_DataTable[j].Privilege)
                    {
                        case 0: type = USER_TYPE.ENGINEER; 
                            break;
                        case 1: type = USER_TYPE.ADMIN;
                            break;
                        case 2: type = USER_TYPE.MOD;
                            break;
                    }
                    if (_DataTable[j].Privilege >= 3) type = USER_TYPE.NORMAL_USER;
                    Current_User = user;
                    break;
                }
            return type;
        }

        public void GetUserList(USER_TYPE type)
        {
            int count = _DataTable.Rows.Count;
            User.Clear();
            Pass.Clear();

            switch (type)
            {
                case USER_TYPE.ENGINEER :
                    for (int j = 0; j < count; j++)
                    {
                        User.Add(_DataTable[j].User);
                        Pass.Add(_DataTable[j].Pass);
                    }
                    break;
                case USER_TYPE.ADMIN:
                    for (int j = 0; j < count; j++)
                    {
                        if (_DataTable[j].Privilege > 0)
                        {
                            User.Add(_DataTable[j].User);
                            Pass.Add(_DataTable[j].Pass);
                        }
                    }
                  
                    break;
                case USER_TYPE.MOD:
                    for (int j = 0; j < count; j++)
                    {
                        if (_DataTable[j].Privilege > 1)
                        {
                            User.Add(_DataTable[j].User);
                            Pass.Add(_DataTable[j].Pass);
                        }
                    }
                  
                    break;
                case USER_TYPE.NORMAL_USER:
                    for (int j = 0; j < count; j++)
                    {
                        if (_DataTable[j].Privilege > 2)
                        {
                            User.Add(_DataTable[j].User);
                            Pass.Add(_DataTable[j].Pass);
                        }
                    }
                    break;
            }
           
        }

        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
        }

        private void UpdateDB()
        {
            try
            {
                _Adt.Update(_DataTable);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
